<template>
    <div class="login">
        <div class="login-container">
            <div class="login-container-text">后台管理系统</div>
            <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
                <el-form-item prop="userName">
                    <el-input v-model.number="ruleForm.userName" placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item prop="pass">
                    <el-input v-model="ruleForm.pass" type="password" autocomplete="off" placeholder="请输入密码" />
                </el-form-item>
                <el-form-item>
                    <el-button class="login-button" type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                </el-form-item>
            </el-form>
            <el-text class="mx-1 register-link" @click="registerHandle">没有账号？立即注册</el-text>
        </div>
    </div>
</template>

<script lang="ts" setup>
/* eslint-disable */
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useRouter } from 'vue-router';
import { UserLogin } from '@/request/api';
import { useLoginInformation } from '@/store/loginInformation';

const ruleFormRef = ref<FormInstance>()
const router = useRouter();
const loginInformationStore = useLoginInformation();
const checkUserName = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('请输入用户名'))
    }
    callback()
}

const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入密码'))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    pass: '',
    userName: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
    pass: [{ validator: validatePass, trigger: 'blur' }],
    userName: [{ validator: checkUserName, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            UserLogin(JSON.stringify({
                userId: ruleForm.userName,
                password: ruleForm.pass
            })).then((res) => {
                console.log(res)
                if (res.status == 200) {
                    Object.assign(loginInformationStore.loginInformation, res.data)
                    localStorage.setItem('token', res.data.token)
                    localStorage.setItem('userName', res.data.userName)
                    localStorage.setItem('userId', res.data.userId)
                    ElMessage.success('登录成功')
                    router.push({ path: "/HomePage" })
                }
            }, (error) => {
                ElMessage.error('登录失败')
                formEl.resetFields()
            })
        } else {
            console.log('error submit!')
            return false
        }
    })
}
function registerHandle() {
    router.push({ path: "/Register" })
}
</script>

<style scoped>
.login {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    width: 100vw;
    background: url(@/assets/login.jpeg) no-repeat;
    background-size: cover;
}

.login-container {
    position: relative;
    width: 400px;
    height: 190px;
    background-color: #f2f2f2;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    padding: 10px;
}

.login-container-text {
    font-size: large;
    margin-bottom: 5px;
}

.login-button {
    width: calc(100% + 0px);
}

.register-link {
    /* 使用绝对定位 */
    position: absolute;
    /* 定位到容器底部 */
    bottom: 10px;
    /* 定位到容器右侧 */
    right: 10px;
    text-decoration: underline;
    cursor: pointer;
}
</style>